-- FETCH_MAGIC_SHOP_GOODS

-- 魔法商店
-- @param user 玩家对象
return function(user)
    local goods = DungeonShopM.getGoodsByType("magic_1");
    local count = DungeonShopM.queryShopRule("magic", "total_count");
    local ret = {};

    -- 商品数量加
    local prop = PropM.combine(user, "shop_num", "magic");
    count = count + PropM.apply(prop, 1);

    -- 固定出现的商品
    for _, data in ipairs(goods) do
        table.insert(ret, { ["goodsId"] = data["id"], });

        count = count - 1;
    end

    -- 需要卖药剂配方
    local goods_2   = DungeonShopM.getGoodsByType("magic_2");
    local dungeonId = DungeonM.getDungeonId();
    local layer     = DungeonM.currentLayer();
    local maxFloor  = DungeonAreaM.query(dungeonId, "max_floor");
    -- 在小关的非最高层，且药剂配方未获得过
    if layer ~= maxFloor then
        local formulaId;
        local reagentId;
        for i = 1, #goods_2 do
            formulaId = goods_2[i].goods[2];
            reagentId = ReagentM.getReagentByFormula(formulaId);
            if goods_2[i].area == dungeonId and ItemM.getAmount(user, formulaId) <= 0 and
               not ReagentM.isReagentActive(reagentId) then
                -- 每关限1个，且不占商品数量
                table.insert(ret, { ["goodsId"] = goods_2[i].id });
                break;
            end
        end
    end

    -- 抽取随机商品
    goods = DungeonShopM.getGoodsByType("magic");

    -- 南瓜马车的特殊处理：29层保底出现
    -- 否则，在10层后正常抽取
    local carriageGood = DungeonShopM.getGoodsByType("magic_carriage");
    if sizeof(carriageGood) > 0 then
        if layer == 29 then
            table.insert(ret, { ["goodsId"] = carriageGood[1].id, });
            count = count - 1;
        elseif layer >= 10 then
            goods = table.append(goods, carriageGood);
        end
    end

    local filterArr = {};
    local rand;
    local good;
    for i = 1, count do
        rand = DungeonM.getRandSeed("shop");

        -- 抽取到一个商品
        good = DungeonShopM.fetchByRand(goods, rand, filterArr);
        ret = table.append(ret, good);

        for _, data in pairs(good) do
            if DungeonShopM.query(data.goodsId, "unrepeat") == 1 then
                -- 避免抽取一样的
                table.insert(filterArr, data.goodsId);
            end
        end
    end

    return ret;
end
